#!/usr/bin/sh

app_name="Oracle-12.2"


# 判断vdata是否存在该文件
vdata_file=$(find /vdata/ -name "$app_name")
if [ -z "$vdata_file" ]; then
    # 创建Oracle-12.2文件夹
    mkdir -p /vdata/$app_name
    # 查找运行路径下有没有该文件，也就是镜像工作区下面
    app=$(find ./ -name "$app_name")
    if [ -n "$app" ]; then
        echo "将文件移动到/vdata/目录下"
        mv "$app" /vdata/
    else
        echo "文件不存在，已退出"
        exit
    fi
fi


# 安装Oracle软件
rpm -ivh /vdata/$app_name/*.rpm

# 编辑Oracle客户端环境变量
tee -a ~/.bash_profile <<-'EOF'

# 配置Oracle环境变量
export ORACLE_HOME=/usr/lib/oracle/12.2/client64 
export TNS_ADMIN=/usr/lib/oracle/12.2/client64 
export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib 
export ORABIN=/usr/lib/oracle/12.2/client64/bin 
export PATH=$PATH:$ORABIN 
export PATH=$ORACLE_HOME:$PATH 
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin

EOF

# 更新环境变量
source ~/.bash_profile 

# 配置TNS内容
mkdir -p /usr/lib/oracle/12.2/client64/network/admin/

tee -a /usr/lib/oracle/12.2/client64/network/admin/tnsnames.ora  <<-'EOF'

# 配置Oracle监听文件
# HOST、PORT、SERVICE_NAME
ORCLPDB1= 
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.124.213.52)(PORT = 9022))
    )
    (CONNECT_DATA =
      (SERVICE_NAME  = ORCLPDB1)
    )
  )

EOF
